﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hibiku.Twitter
{
    public static class TextCodec
    {
        private static readonly Dictionary<string, string> s_encodeMap = new Dictionary<string, string>()
                                                                                {
                                                                                   {"<", "&lt;"},
                                                                                   {">", "&gt;"}
                                                                                };

        private static readonly Dictionary<string, string> s_decodeMap = s_encodeMap.ToDictionary(x => x.Value, y => y.Key);

        public static string Encode(string text)
        {
            var buffer = new StringBuilder(text);

            foreach (var pair in s_encodeMap)
            {
                buffer.Replace(pair.Key, pair.Value);
            }

            return buffer.ToString();
        }

        public static string Decode(string text)
        {
            var buffer = new StringBuilder(text);

            foreach (var pair in s_decodeMap)
            {
                buffer.Replace(pair.Key, pair.Value);
            }

            return buffer.ToString();
        }
    }
}
